var defaultID = undefined;
var defaultName = undefined;

$(document).ready(function () {
    doGetAllMembers('memberPackUp','backLogMembers','memberUnfurled');
    doGetUser();
    privileges();
});
function privileges(){
    var ua = navigator.userAgent;
    var ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
    isIphone =!ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
    isAndroid = ua.match(/(Android)\s+([\d.]+)/),
    isMobile = isIphone || isAndroid;
    if(isMobile){
      $("#hideShowMainMenuBtn",parent.document).css('left','15pt');
      $("#sprintCompletion>div>a").css("padding-left","16px");
      if(document.title == "Back Log"){
         linkTag = $('<link href="../PM/css/backLogPhone.css" rel="stylesheet" type="text/css" />');
         $($('head')[0]).append(linkTag);
      }else if(document.title == "产品"){
         linkTag = $('<link href="../PM/css/productPnone.css" rel="stylesheet" type="text/css" />');
         $($('head')[0]).append(linkTag);
      }else if(document.title == "反馈"){
         linkTag = $('<link href="../PM/css/productPnone.css" rel="stylesheet" type="text/css" />');
         $($('head')[0]).append(linkTag);
         linkTag = $('<link href="../PM/css/feedbackPhone.css" rel="stylesheet" type="text/css" />');
         $($('head')[0]).append(linkTag);
      }else if(document.title == "Sprint"){
           linkTag = $('<link href="../PM/css/productPnone.css" rel="stylesheet" type="text/css" />');
           $($('head')[0]).append(linkTag);
           linkTag = $('<link href="../PM/css/sprintPhone.css" rel="stylesheet" type="text/css" />');
           $($('head')[0]).append(linkTag);
      }
      if(isIphone){
        if(document.title == "反馈"){
          $("#BackLogRightMenu").css("width","1000pt");
          $("#top",parent.document).css("width","1000pt");
        }
      }
    }else{
       $("#hideShowMainMenuBtn",parent.document).hide();
    }
}
function doGetUser(){
    $.post("doGetUser",function (result) {
        if(result.state==1){
            var list=result.data;
            defaultID=list.userID;
            defaultName=list.userName;
            $("#userName").html(defaultName);
        }else{
            showMessage("获取登录用户信息失败")
        }
    })
}
function doneEffect(){
    var ua = navigator.userAgent;
    var ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
    isIphone =!ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
    isAndroid = ua.match(/(Android)\s+([\d.]+)/),
    isMobile = isIphone || isAndroid;
    if(isMobile){
       $("#BackLogLeftMenu").hide();
       $("#BackLogRightMenu").show();
    }
}
function formatDate(v) {
        if (/^(-)?\d{1,10}$/.test(v)) {
            v = v * 1000;
        } else if (/^(-)?\d{1,13}$/.test(v)) {
            v = v * 1;
        }
        var dateObj = new Date(v);
        var month = dateObj.getMonth() + 1;
        var day = dateObj.getDate();
        var hours = dateObj.getHours();
        var minutes = dateObj.getMinutes();
        var seconds = dateObj.getSeconds();
        if (month < 10) {
            month = "0" + month;
        }
        if (day < 10) {
            day = "0" + day;
        }
        if (hours < 10) {
            hours = "0" + hours;
        }
        if (minutes < 10) {
            minutes = "0" + minutes;
        }
        if (seconds < 10) {
            seconds = "0" + seconds;
        }
        var UnixTimeToDate = dateObj.getFullYear() + '-' + month + '-' +day;
    /*var UnixTimeToDate = dateObj.getFullYear() + '-' + month + '-' +day + ' ' + hours + ':' + minutes + ':' + seconds;*/
    return UnixTimeToDate;
}
function doGetAllMembers(OneElement,TwoElement,ThreeElement){
    $.post("doGetAllMembers",function (result) {
        if(result.state==1){
            var list=result.data;
            var backLogMembers = $("#backLogMembers");
            backLogMembers.empty();
            for(var i in list){
                var memberInitials=list[i].userName.substring(0,1);
                backLogMembers.append('<div class="memberList" id="'+list[i].userID+'">'+
                    '<span class="memberLabel">'+memberInitials+'</span>'+
                    '<span id="'+list[i].userID+'span" class="memberName commonBlueFont"  onMouseOver="mmigration(\''+ list[i].userID +'span\')" onMouseOut="shiftOut()" onclick="getBackLogByUser(\''+ list[i].userID +'\',\'2\')">'+list[i].userName+'</span></div>');
            }
            if (defaultID) {
                $("#" + defaultID + "span").addClass("activeSpan");
            }
            $("#"+ThreeElement).hide();
            $('#'+OneElement).show();
            $('#'+TwoElement).show();
        }else{
            showMessage("获取成员失败");
        }
        setUserNameStyle();
    });
}
function toCloseVersion(productId,OneElement){
    var $Id=$(OneElement).attr("id");
    $(OneElement).hide();
    var $Id2=$Id.substr(0,$Id.length-1)+"1";
    $("#"+$Id2).show();
    $("#"+productId).hide();
}
function setUserNameStyle() {
    var username=defaultID;
    var MemberId=$(".memberList").children("span:last-child");
    for(var i=0;i<MemberId.length;i++){
        if(MemberId[i].id==username){
            $(MemberId[i]).addClass("activeSpan");
        }
    }
    var $div=$(".memberList");
    for (var i = 0; i < $div.length; i++) {
        $div[i].setAttribute('ondragenter', 'dragEnter(event)');
        $div[i].setAttribute('ondragleave', 'dragLeave(event)');
        $div[i].setAttribute('ondrop', 'drop(event)');
        $div[i].setAttribute('ondragover', 'allowDrop(event)');
    }
}
function closeModal(modalDiv){
    $("#"+modalDiv).hide();
}
function toClose(OneElement,TwoElement,ThreeElement){
    $("#"+ThreeElement).hide();
    $("#"+TwoElement).hide();
    $("#"+OneElement).show();
}

function selectDate(ID, minView, pickerPosition, startView){
    $('#' + ID).datetimepicker({
        language: 'zh-CN', // String. 默认值: 'en'
        weekStart: 1, // Integer. 默认值：0;一周从哪一天开始。0（星期日）到6（星期六）
        todayBtn:  "linked", // Boolean, "linked". 默认值: false;
        todayHighlight: true, // Boolean. 默认值: false;如果为true, 高亮当前日期。
        autoclose: true, // Boolean. 默认值：false;当选择一个日期之后是否立即关闭此日期时间选择器。
        startView: startView == undefined ? 2 : startView, // Number, String. 默认值：2, 'month';日期时间选择器打开之后首先显示的视图。[0/'hour',1/'day',2/'month',3/'year',4/'decade']
        minView: minView == undefined ? 2 : minView, // Number, String. 默认值：0, 'hour';日期时间选择器所能够提供的最精确的时间选择视图。
        forceParse: true, // Boolean. 默认值: true;当选择器关闭的时候，是否强制解析输入框中的值
        pickerPosition: pickerPosition || 'bottom-left', // String. 默认值: 'bottom-right';通过设置选项可以讲选择器放倒输入框下方
    });
}
function stringToDate(date, symbol){
    if(date.length <= 10){
        if (!symbol) {
            symbol = "-";
        }
        var array = date.split(symbol);
        return new Date(array[0], array[1] - 1, array[2]);
    } else {
        var array = date.split(/[^0-9]/ig);
        return new Date(array[0], array[1] - 1, array[2], array[3] || 0, array[4] || 0, array[5] || 0);
    }
}
function IntToDay(data){
    return data/1000/3600/24;
}
//数据库时间转化为JSON在页面显示
function timeStamp2String(time, model){
    if (time) {
        var datetime =  new Date();
        datetime.setTime(time);
        var year = datetime.getFullYear();
        var month = datetime.getMonth() + 1 < 10 ? (datetime.getMonth() + 1) : "" + (datetime.getMonth() + 1);
        var date = datetime.getDate() < 10 ? datetime.getDate() : "" + datetime.getDate();
        var hour = datetime.getHours()< 10 ? datetime.getHours() : "" + datetime.getHours();
        var minute = datetime.getMinutes()< 10 ? datetime.getMinutes() : "" + datetime.getMinutes();
        var second = datetime.getSeconds()< 10 ? datetime.getSeconds() : "" + datetime.getSeconds();
        if (model == 1) {
            return year + month + date + hour + minute + second;
        }else if (model == 2) {
            return year + "-" + month + "-" + date;
        }else if (model == 3) {
            return month + "月" + date + "日" ;
        }else {
            return year + "-" + month + "-" + date +" "+ hour + ":" + minute + ":" + second;
        }
    }else{
        return "";
    }
}
function mmigration(ID){
    $(".commonBlueFont").removeClass("fontComClass");
    $("#" + ID).addClass("fontComClass");
}
function shiftOut(){
    $(".commonBlueFont").removeClass("fontComClass");
}
function showMessage(message, num, methodName) {
    $("#promptMessage", parent.document).show();
    $("#promptMsg", parent.document).html(message);
    $("#doubleSure", parent.document).hide();
    $("#onlySure", parent.document).show();
    if (num ==  1) {
        $("#sureBtn", parent.document).attr("onClick", methodName);
        $("#onlySure", parent.document).hide();
        $("#doubleSure", parent.document).show();
    }
}
function showHideModal(){
    $("#doubleSure", parent.document).hide();
    $("#onlySure", parent.document).show();
    $("#promptMsg", parent.document).html('');
    $("#promptMessage", parent.document).hide();
}
//两个时间相差天数 兼容firefox chrome
//sDate1和sDate2是2006-12-18格式
function datedifference(sDate1, sDate2) {
    var dateSpan,tempDate,iDays;
    sDate1 = Date.parse(sDate1);
    sDate2 = Date.parse(sDate2);
    dateSpan = sDate2 - sDate1;
    dateSpan = Math.abs(dateSpan);
    iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
    return iDays
};